package com.yyds.yygh.hospclient.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yyds.yygh.cmnclient.DictFeignClient;
import com.yyds.yygh.hospclient.repository.HospitalRepository;
import com.yyds.yygh.hospclient.service.HospitalService;
import com.yyds.yygh.model.hosp.BookingRule;
import com.yyds.yygh.model.hosp.Hospital;
import com.yyds.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> map) {
        Hospital hospital = JSONObject.parseObject(JSON.toJSONString(map), Hospital.class);
        //根据hoscode查询医院是否存在
        Hospital hasHospital = hospitalRepository.findByHoscode(hospital.getHoscode());
        if (hasHospital == null){
            //如果医院不存在则进行添加操作
            hospital.setStatus(0);
            Date time = new Date();
            hospital.setCreateTime(time);
            hospital.setUpdateTime(time);
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else {
            //如果医院存在则进行修改操作
            hasHospital.setIsDeleted(0);
            hasHospital.setUpdateTime(new Date());
            hospitalRepository.save(hasHospital);
        }

    }

    @Override
    public Hospital findHospital(String hoscode) {
        return hospitalRepository.findByHoscode(hoscode);
    }

    @Override
    public Page<Hospital> selectPage(Integer pageNum, Integer pageSize, HospitalQueryVo hospitalQueryVo) {
        //根据创建时间倒序查询
        Sort sort = Sort.by(Sort.Direction.DESC,"createTime");
        //类属性转换
        Hospital hospital = new Hospital();
        if (hospitalQueryVo!=null){
            BeanUtils.copyProperties(hospitalQueryVo,hospital);
        }
        //配置页码
        Pageable pageable = PageRequest.of(pageNum - 1 , pageSize , sort);
        //mongoDB模糊查询
        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        //进行查询
        Example<Hospital> example = Example.of(hospital, matcher);
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        for (Hospital hosp : pages) {
            this.packHospital(hosp);
        }
        return pages;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String, Object> showDetail(String id) {
        Hospital hospital = hospitalRepository.findById(id).get();
        Map<String, Object> map = new HashMap<>();
        BookingRule bookingRule = hospital.getBookingRule();
        map.put("bookingRule",bookingRule);
        map.put("hospital",this.packHospital(hospital));
        hospital.setBookingRule(null);
        return map;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.findByHoscode(hoscode);
        return hospital.getHosname();
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        List<Hospital> hospitalList = hospitalRepository.findByHosnameLike(hosname);
        return hospitalList;
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        //根据医院编号查询医院
        Hospital hospital = hospitalRepository.findByHoscode(hoscode);
        //将查询的医院设置省份城市以及医院等级的信息
        hospital = this.packHospital(hospital);
        Map<String,Object> map = new HashMap<>();
        //获取部门规则
        BookingRule bookingRule = hospital.getBookingRule();
        map.put("bookingRule",bookingRule);
        map.put("hospital",hospital);
        //避免重复获取规则
        hospital.setBookingRule(null);
        return map;
    }

    private Hospital packHospital(Hospital hospital){
        String proviceName = dictFeignClient.getName(hospital.getProvinceCode());
        String cityName = dictFeignClient.getName(hospital.getCityCode());
        String districtName = dictFeignClient.getName(hospital.getDistrictCode());
        String hospGrade = dictFeignClient.getName("Hostype",hospital.getHostype());
        hospital.getParam().put("hostypeName",proviceName+cityName+districtName);
        hospital.getParam().put("fullAddress",hospGrade);
        return hospital;
    }
}
